package com.ht.modules.component.ai.tools.service;

import com.ht.modules.alarmstatis.entity.AlarmMaster;
import com.ht.modules.alarmstatis.entity.AlarmMasterVo;
import com.ht.modules.alarmstatis.service.IAlarmStatisService;
import com.ht.modules.component.ai.embedding.EmbeddingService;
import com.ht.modules.component.ai.tools.entity.ResponseToolsData;
import com.ht.modules.customerfirminfo.entity.CustomerFirmInfo;
import com.ht.modules.customerfirminfo.utils.CustomerFirmInfoCrawlerUtil;
import com.ht.modules.device.entity.DateUtils;
import com.ht.modules.device.entity.DeviceMonitorRecords;
import com.ht.modules.device.entity.DeviceMonitorRecordsVo;
import com.ht.modules.device.mapper.DeviceMonitorRecordsMapper;
import com.ht.modules.device.service.IDeviceMonitorRecordsService;
import com.ht.modules.weather.entity.WeatherCityInfo;
import com.ht.modules.weather.entity.WeatherDetails;
import com.ht.modules.weather.entity.WeatherDto;
import com.ht.modules.weather.entity.WeatherInfo;
import com.ht.modules.weather.service.WeatherService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.agent.tool.ToolMemoryId;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;


@Slf4j
@Component
public class ToolService {
    @Autowired
    private IDeviceMonitorRecordsService deviceMonitorRecordsService;
    @Autowired
    private CustomerFirmInfoCrawlerUtil customerFirmInfoCrawlerUtil;
    @Autowired
    private WeatherService weatherService;
    @Autowired
    private EmbeddingService embeddingService;
    @Autowired
    private DeviceMonitorRecordsMapper deviceMonitorRecordsMapper;
    @Autowired
    private IAlarmStatisService alarmStatisService;

    private final Map<String, Integer> cityCode = new HashMap<>(16);

    @Tool(name = "报警总表工具：", value = "根据所属、名称、位号来源、报警开始结束时间等字段来进行报警总表分析：")
    public ResponseToolsData<?> alarmMasterList(
            @ToolMemoryId String toolMemoryId,
            @P(value = "所属", required = false) String source,
            @P(value = "名称", required = false) String name,
            @P(value = "位号来源", required = false) String pointFrom,
            @P(value = "报警开始时间") String startTime,
            @P(value = "报警结束时间") String endTime
    ) {
        System.out.println("toolMemoryId = " + toolMemoryId);
        AlarmMaster alarmMaster = new AlarmMaster();
        alarmMaster.setPointFrom(pointFrom);
        alarmMaster.setStartTime(DateUtils.parseDate(startTime + " 00:00:00", "yyyy-MM-dd HH:mm:ss"));
        alarmMaster.setEndTime(DateUtils.parseDate(endTime + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        if (Strings.isNotBlank(source)) {
            alarmMaster.setCentreName(name);
            String tableName;
            if (source.contains("中心")) {
                tableName = "aimp_centre";
            } else if (source.contains("装置")) {
                tableName = "aimp_device";
            } else if (source.contains("单元")) {
                tableName = "aimp_unit";
            } else {
                return ResponseToolsData.<AlarmMasterVo>builder().name("===========月周统计报警工具==========").data(Collections.emptyList()).build();
            }
            Map<String, Integer> source1 = deviceMonitorRecordsMapper.source(tableName, name);
            if (source1.isEmpty()) {
                return ResponseToolsData.<AlarmMasterVo>builder().name("===========月周统计报警工具==========").data(Collections.emptyList()).build();
            }
            Integer id = source1.get("id");
            if (source.contains("中心")) {
                alarmMaster.setCentreId(id);
            } else if (source.contains("装置")) {
                alarmMaster.setDeviceId(id);
            } else if (source.contains("单元")) {
                alarmMaster.setUnitId(id);
            } else {
                return ResponseToolsData.<AlarmMasterVo>builder().name("===========月周统计报警工具==========").data(Collections.emptyList()).build();
            }
        }
        List<AlarmMasterVo> alarmMasterVos = alarmStatisService.alarmMasterList(alarmMaster);
        List<Map<String, Object>> dataMap = new ArrayList<>();
        alarmMasterVos.forEach(item -> {
            List<AlarmMaster> data = item.getData();
            for (AlarmMaster datum : data) {
                Map<String, Object> da = new HashMap<>();
                da.put("中心名称", datum.getCentreName());
                da.put("装置名称", datum.getDeviceName());
                da.put("单元名称", datum.getUnitName());
                da.put("报警次数", datum.getAlarmCount());
                da.put("处置次数", datum.getDisposeCount());
                da.put("处置率", datum.getDisposeRate());
                da.put("间隔天数", datum.getIntervalDay());
                da.put("日均报警", datum.getDayAlarmAvg());
                da.put("环比报警次数", datum.getChainCount());
                da.put("环比变化", datum.getChainRatio());
                da.put("趋势", "red".equals(datum.getColor()) ? "上升" : "下降");
                dataMap.add(da);
            }
        });
        return ResponseToolsData.<Map<String, Object>>builder().name("===========月周统计报警工具==========").data(dataMap).build();
    }


    @Tool("截止时间xx，n天前的报警数量工具：")
    public ResponseToolsData<?> yearMonthWeekStatistics(@P("截止哪天") String dataTime, @P("查询多少天") Integer days) {
        List<Map<String, Object>> data = new ArrayList<>();
        if (Strings.isBlank(dataTime) || null == days) {
            Map<String, Object> maps = new HashMap<>();
            maps.put("结果", "请明确提供查询类截止哪天，查询多少天相关信息");
            data.add(maps);
        }
        List<Map<String, Integer>> maps = deviceMonitorRecordsMapper.yearMonthWeekStatistics(dataTime, days);
        maps.forEach(item -> {
            data.add(Map.of("报警时间", item.get("报警时间"), "报警数量", item.get("报警数量")));
        });
        return ResponseToolsData.<Map<String, Object>>builder().name("===========月周统计报警工具==========").data(data).build();
    }

    // 列出设备‘粉剂1号线温度’，在时间2025-05-12 16:40:00到时间2025-05-12 16:50:00之间的监测数据，并做简单数据分析
    // 列出设备‘粉剂1号线温度’，在时间2025-05-12 16:40:00到时间2025-05-12 16:50:00之间的监测数据
    @Tool("根据设备名称，采集时间点查询设备数据工具：")
    public ResponseToolsData<?> deviceMonitorRecords(@P("设备名称") String deviceName, @P("时间") String dataTime) {
        List<String> data = new ArrayList<>();
        if (Strings.isBlank(deviceName)) {
            data.add("请明确提供已有设备名称相关信息");
        }
        if (Strings.isBlank(dataTime)) {
            data.add("请明确提供时间点相关信息");
        }
        if (data.isEmpty()) {
            DeviceMonitorRecordsVo deviceMonitorRecordsVo = new DeviceMonitorRecordsVo();
            deviceMonitorRecordsVo.setDeviceName(deviceName);
            deviceMonitorRecordsVo.setDateTimeStr(dataTime);
            deviceMonitorRecordsVo.setPartitionDate(dataTime.replaceAll("-", "").substring(0, 6));
            List<DeviceMonitorRecordsVo> deviceMonitorRecordsVos = deviceMonitorRecordsService.listNoPage(deviceMonitorRecordsVo, "dateTime", "asc");
            for (int i = 0; i < deviceMonitorRecordsVos.size(); i++) {
                data.add((i + 1) + ".时间:" + DateUtils.formatDate(deviceMonitorRecordsVos.get(i).getDateTime(), "yyyy-MM-dd HH:mm:ss") + ";数据:" + deviceMonitorRecordsVos.get(i).getValue());
            }
        }
        return ResponseToolsData.<String>builder().name("===========电设备工具-查询单独时间点==========").data(data).build();
    }

    @Tool("根据设备名称，开始时间，结束时间查询设备数据工具：")
    public ResponseToolsData<?> deviceMonitorRecordsList(@P("设备名称") String deviceName, @P("开始时间") String startTime, @P("结束时间") String endTime) {
        List<String> data = new ArrayList<>();
        if (Strings.isBlank(deviceName)) {
            data.add("请明确提供已有设备名称相关信息");
        }
        if (Strings.isBlank(startTime) || Strings.isBlank(endTime)) {
            data.add("请明确提供起止时间点相关信息");
        }
        if (data.isEmpty()) {
            DeviceMonitorRecordsVo deviceMonitorRecordsVo = new DeviceMonitorRecordsVo();
            deviceMonitorRecordsVo.setDeviceName(deviceName);
            deviceMonitorRecordsVo.setStartTime(startTime);
            deviceMonitorRecordsVo.setEndTime(endTime);
            deviceMonitorRecordsVo.setPartitionDate(startTime.replaceAll("-", "").substring(0, 6));
            List<DeviceMonitorRecordsVo> deviceMonitorRecordsVos = deviceMonitorRecordsService.listNoPage(deviceMonitorRecordsVo, "dateTime", "asc");
            for (int i = 0; i < deviceMonitorRecordsVos.size(); i++) {
                data.add((i + 1) + ".时间:" + DateUtils.formatDate(deviceMonitorRecordsVos.get(i).getDateTime(), "yyyy-MM-dd HH:mm:ss") + ";数据:" + deviceMonitorRecordsVos.get(i).getValue());
            }
        }
        return ResponseToolsData.<String>builder().name("===========电设备工具-查询==========").data(data).build();
    }

    @Tool("根据设备名称，时间修改设备数据工具：")
    public ResponseToolsData<?> editDeviceMonitorRecords(@P("设备名称") String deviceName, @P("时间") String dateTime, @P("修改值") String value) {
        List<String> data = new ArrayList<>();
        if (Strings.isBlank(deviceName)) {
            data.add("请明确提供已有设备名称相关信息");
        }
        if (Strings.isBlank(dateTime)) {
            data.add("请明确提供时间点相关信息");
        }
        if (Strings.isBlank(value)) {
            data.add("请明确提供要修改的数据值相关信息");
        }
        DeviceMonitorRecordsVo deviceMonitorRecordsVo = new DeviceMonitorRecordsVo();
        deviceMonitorRecordsVo.setDeviceName(deviceName);
        deviceMonitorRecordsVo.setDateTimeStr(dateTime);
        deviceMonitorRecordsVo.setPartitionDate(dateTime.replaceAll("-", "").substring(0, 6));
        List<DeviceMonitorRecordsVo> deviceMonitorRecordsVos = deviceMonitorRecordsService.listNoPage(deviceMonitorRecordsVo, "dateTime", "asc");
        if (deviceMonitorRecordsVos.isEmpty()) {
            data.add("未找到到相关记录");
        }
        if (data.isEmpty()) {
            deviceMonitorRecordsVos.forEach(item -> {
                        DeviceMonitorRecords deviceMonitorRecords = new DeviceMonitorRecords();
                        deviceMonitorRecords.setId(item.getId());
                        deviceMonitorRecords.setValue(new BigDecimal(value));
                        deviceMonitorRecordsService.edit(deviceMonitorRecords);
                    }
            );
            data.add("操作成功");
        }
        return ResponseToolsData.<String>builder().name("===========电设备工具-修改==========").data(data).build();
    }

    @Tool("根据设备名称，时间移除设备数据工具：")
    public ResponseToolsData<?> removeDeviceMonitorRecords(@P("设备名称") String deviceName, @P("时间") String dateTime) {
        List<String> data = new ArrayList<>();
        if (Strings.isBlank(deviceName)) {
            data.add("请明确提供已有设备名称相关信息");
        }
        if (Strings.isBlank(dateTime)) {
            data.add("请明确提供时间点相关信息");
        }
        DeviceMonitorRecordsVo deviceMonitorRecordsVo = new DeviceMonitorRecordsVo();
        deviceMonitorRecordsVo.setDeviceName(deviceName);
        deviceMonitorRecordsVo.setDateTimeStr(dateTime);
        deviceMonitorRecordsVo.setPartitionDate(deviceMonitorRecordsVo.getDateTimeStr().replaceAll("-", "").substring(0, 6));
        List<DeviceMonitorRecordsVo> deviceMonitorRecordsVos = deviceMonitorRecordsService.listNoPage(deviceMonitorRecordsVo, "dateTime", "asc");
        if (deviceMonitorRecordsVos.isEmpty()) {
            data.add("未找到到相关记录");
        }
        if (data.isEmpty()) {
            deviceMonitorRecordsService.removeBatchByIds(deviceMonitorRecordsVos.stream().map(DeviceMonitorRecordsVo::getId).toList());
            data.add("操作成功");
        }
        return ResponseToolsData.<String>builder().name("===========电设备工具-删除==========").data(data).build();
    }

    @Tool("根据病症名称检索病症具体信息工具：")
    public ResponseToolsData<?> diseaseAnalysis(@P("病症名称") String message) {
        List<String> data = new ArrayList<>();
        if (Strings.isBlank(message)) {
            data.add("请明确提供病症相关信息");
        }
        if (data.isEmpty()) {
            List<String> list = embeddingService.vectorStoreQuery(message, "disease", 0.85, 1);
            if (list.isEmpty()) {
                data.add("未查询到相关数据");
            } else {
                data.addAll(list);
            }
        }
        return ResponseToolsData.<String>builder().name("===========病症工具==========").data(data).build();
    }

    @Tool("获取当前时间工具：")
    public ResponseToolsData<?> getCurrentTime() {
        Date date = new Date();
        String content = DateUtils.formatDate(date, "yyyy-MM-dd HH:mm:ss");
        content = "当前时间：" + content + "\n星期：" + DateUtils.getWeekOfDate(date);
        return ResponseToolsData.builder().name("===========获取当前时间工具==========").data(Collections.singletonList(content)).build();
    }

    @Tool("根据企业名称关键字检索企业信息查询工具：")
    public ResponseToolsData<?> queryCompanyInfo(@P("企业名称包含的关键字") String message) {
        List<Map<String, String>> data = new ArrayList<>();
        if (Strings.isBlank(message)) {
            data.add(Map.of("", "请明确提供包含企业名称的相关信息"));
        }
        if (data.isEmpty()) {
            CustomerFirmInfo customerFirmInfo = new CustomerFirmInfo().setCompanyName(message);
            List<CustomerFirmInfo> customerFirmInfos = customerFirmInfoCrawlerUtil.searchEnterpriseInfo(customerFirmInfo);
            List<Map<String, String>> collect = customerFirmInfos.stream().map(item -> {
                Map<String, String> map = new HashMap<>();
                map.put("单位名称", item.getCompanyName());
                map.put("历史名称", item.getCompanyNameHis());
                map.put("企业规模", item.getCompanySize());
                map.put("联系人", item.getContactPerson());
                map.put("联系方式", item.getContactInformation());
                map.put("企业地址", item.getCompanyAddress());
                // map.put("企业网址", item.getCompanyWebsite());
                // map.put("信用网址", item.getCreditWebsite());
                map.put("统一社会信用代码", item.getUnifyTheSocialCreditCode());
                map.put("注册号", item.getRegistrationNumber());
                map.put("法定代表人", item.getLegalRepresentative());
                map.put("注册资本", item.getRegisteredCapital());
                map.put("成立日期", item.getDateOfEstablishment());
                map.put("营业状态", item.getBusinessStatus());
                // map.put("核准日期", item.getDateOfApproval());
                // map.put("登记机关", item.getRegistrationAuthority());
                // map.put("营业期限自", item.getOperatingTermFrom());
                // map.put("营业期限至", item.getOpenUntil());
                map.put("注册地址", item.getRegisteredAddress());
                map.put("主营产品", item.getMainProduct());
                map.put("单位简介", item.getCompanyProfile());
                return map;
            }).toList();
            data.addAll(collect);
        }
        return ResponseToolsData.<Map<String, String>>builder().name("===========企业信息查询工具==========").data(data).build();
    }

    @Tool("查询某个地区未来15天的天气工具：")
    public ResponseToolsData<?> queryWeather(@P("需要查询天气的城市名称") String cityName) {
        List<String> data = new ArrayList<>();
        if (Strings.isBlank(cityName)) {
            data.add("请明确提供包含城市名称的相关信息");
        }
        if (data.isEmpty()) {
            AtomicInteger code = new AtomicInteger();
            cityCode.forEach((
                    key, value) -> {
                if (cityName.contains(key)) {
                    code.set(value);
                }
            });
            WeatherDto weatherDto = weatherService.getWeatherByCityCode(String.valueOf(code));
            if (null == weatherDto) {
                data.add("未查询到相关数据");
            } else {
                WeatherCityInfo cityInfo = weatherDto.getCityInfo();
                String city = cityInfo.getCity();
                WeatherInfo weatherInfo = weatherDto.getData();
                List<WeatherDetails> weatherDetailsList = weatherInfo.getForecast();
                int index = 1;
                for (WeatherDetails weatherDetails : weatherDetailsList) {
                    String content = index + ".日期：" + weatherDetails.getYmd() +
                            ",城市：" + city +
                            ",星期:" + weatherDetails.getWeek() +
                            ",最高温度:" + weatherDetails.getHigh() +
                            ",最低温度:" + weatherDetails.getLow() +
                            ",空气质量指数:" + weatherDetails.getAqi() +
                            ",风向:" + weatherDetails.getFx() +
                            ",风向级数:" + weatherDetails.getFl() +
                            ",天气:" + weatherDetails.getType() +
                            ",温馨提示:" + weatherDetails.getNotice();
                    data.add(content);
                    index++;
                }
            }
        }
        return ResponseToolsData.<String>builder().name("===========天气查询工具==========").data(data).build();
    }

    @PostConstruct
    private void init() {
        cityCode.put("北京", 101010100);
        cityCode.put("朝阳", 101010300);
        cityCode.put("顺义", 101010400);
        cityCode.put("怀柔", 101010500);
        cityCode.put("通州", 101010600);
        cityCode.put("昌平", 101010700);
        cityCode.put("延庆", 101010800);
        cityCode.put("丰台", 101010900);
        cityCode.put("石景山", 101011000);
        cityCode.put("大兴", 101011100);
        cityCode.put("房山", 101011200);
        cityCode.put("密云", 101011300);
        cityCode.put("门头沟", 101011400);
        cityCode.put("平谷", 101011500);
        cityCode.put("八达岭", 101011600);
        cityCode.put("佛爷顶", 101011700);
        cityCode.put("汤河口", 101011800);
        cityCode.put("密云上甸子", 101011900);
        cityCode.put("斋堂", 101012000);
        cityCode.put("霞云岭", 101012100);
        cityCode.put("北京城区", 101012200);
        cityCode.put("海淀", 101010200);
        cityCode.put("天津", 101030100);
        cityCode.put("宝坻", 101030300);
        cityCode.put("东丽", 101030400);
        cityCode.put("西青", 101030500);
        cityCode.put("北辰", 101030600);
        cityCode.put("蓟县", 101031400);
        cityCode.put("汉沽", 101030800);
        cityCode.put("静海", 101030900);
        cityCode.put("津南", 101031000);
        cityCode.put("塘沽", 101031100);
        cityCode.put("大港", 101031200);
        cityCode.put("武清", 101030200);
        cityCode.put("宁河", 101030700);
        cityCode.put("上海", 101020100);
        cityCode.put("宝山", 101020300);
        cityCode.put("嘉定", 101020500);
        cityCode.put("南汇", 101020600);
        cityCode.put("浦东", 101021300);
        cityCode.put("青浦", 101020800);
        cityCode.put("松江", 101020900);
        cityCode.put("奉贤", 101021000);
        cityCode.put("崇明", 101021100);
        cityCode.put("徐家汇", 101021200);
        cityCode.put("闵行", 101020200);
        cityCode.put("金山", 101020700);
        cityCode.put("石家庄", 101090101);
        cityCode.put("张家口", 101090301);
        cityCode.put("承德", 101090402);
        cityCode.put("唐山", 101090501);
        cityCode.put("秦皇岛", 101091101);
        cityCode.put("沧州", 101090701);
        cityCode.put("衡水", 101090801);
        cityCode.put("邢台", 101090901);
        cityCode.put("邯郸", 101091001);
        cityCode.put("保定", 101090201);
        cityCode.put("廊坊", 101090601);
        cityCode.put("郑州", 101180101);
        cityCode.put("新乡", 101180301);
        cityCode.put("许昌", 101180401);
        cityCode.put("平顶山", 101180501);
        cityCode.put("信阳", 101180601);
        cityCode.put("南阳", 101180701);
        cityCode.put("开封", 101180801);
        cityCode.put("洛阳", 101180901);
        cityCode.put("商丘", 101181001);
        cityCode.put("焦作", 101181101);
        cityCode.put("鹤壁", 101181201);
        cityCode.put("濮阳", 101181301);
        cityCode.put("周口", 101181401);
        cityCode.put("漯河", 101181501);
        cityCode.put("驻马店", 101181601);
        cityCode.put("三门峡", 101181701);
        cityCode.put("济源", 101181801);
        cityCode.put("安阳", 101180201);
        cityCode.put("合肥", 101220101);
        cityCode.put("芜湖", 101220301);
        cityCode.put("淮南", 101220401);
        cityCode.put("马鞍山", 101220501);
        cityCode.put("安庆", 101220601);
        cityCode.put("宿州", 101220701);
        cityCode.put("阜阳", 101220801);
        cityCode.put("亳州", 101220901);
        cityCode.put("黄山", 101221001);
        cityCode.put("滁州", 101221101);
        cityCode.put("淮北", 101221201);
        cityCode.put("铜陵", 101221301);
        cityCode.put("宣城", 101221401);
        cityCode.put("六安", 101221501);
        cityCode.put("巢湖", 101221601);
        cityCode.put("池州", 101221701);
        cityCode.put("蚌埠", 101220201);
        cityCode.put("杭州", 101210101);
        cityCode.put("舟山", 101211101);
        cityCode.put("湖州", 101210201);
        cityCode.put("嘉兴", 101210301);
        cityCode.put("金华", 101210901);
        cityCode.put("绍兴", 101210501);
        cityCode.put("台州", 101210601);
        cityCode.put("温州", 101210701);
        cityCode.put("丽水", 101210801);
        cityCode.put("衢州", 101211001);
        cityCode.put("宁波", 101210401);
        cityCode.put("重庆", 101040100);
        cityCode.put("合川", 101040300);
        cityCode.put("南川", 101040400);
        cityCode.put("江津", 101040500);
        cityCode.put("万盛", 101040600);
        cityCode.put("渝北", 101040700);
        cityCode.put("北碚", 101040800);
        cityCode.put("巴南", 101040900);
        cityCode.put("长寿", 101041000);
        cityCode.put("黔江", 101041100);
        cityCode.put("万州天城", 101041200);
        cityCode.put("万州龙宝", 101041300);
        cityCode.put("涪陵", 101041400);
        cityCode.put("开县", 101041500);
        cityCode.put("城口", 101041600);
        cityCode.put("云阳", 101041700);
        cityCode.put("巫溪", 101041800);
        cityCode.put("奉节", 101041900);
        cityCode.put("巫山", 101042000);
        cityCode.put("潼南", 101042100);
        cityCode.put("垫江", 101042200);
        cityCode.put("梁平", 101042300);
        cityCode.put("忠县", 101042400);
        cityCode.put("石柱", 101042500);
        cityCode.put("大足", 101042600);
        cityCode.put("荣昌", 101042700);
        cityCode.put("铜梁", 101042800);
        cityCode.put("璧山", 101042900);
        cityCode.put("丰都", 101043000);
        cityCode.put("武隆", 101043100);
        cityCode.put("彭水", 101043200);
        cityCode.put("綦江", 101043300);
        cityCode.put("酉阳", 101043400);
        cityCode.put("秀山", 101043600);
        cityCode.put("沙坪坝", 101043700);
        cityCode.put("永川", 101040200);
        cityCode.put("福州", 101230101);
        cityCode.put("泉州", 101230501);
        cityCode.put("漳州", 101230601);
        cityCode.put("龙岩", 101230701);
        cityCode.put("晋江", 101230509);
        cityCode.put("南平", 101230901);
        cityCode.put("厦门", 101230201);
        cityCode.put("宁德", 101230301);
        cityCode.put("莆田", 101230401);
        cityCode.put("三明", 101230801);
        cityCode.put("兰州", 101160101);
        cityCode.put("平凉", 101160301);
        cityCode.put("庆阳", 101160401);
        cityCode.put("武威", 101160501);
        cityCode.put("金昌", 101160601);
        cityCode.put("嘉峪关", 101161401);
        cityCode.put("酒泉", 101160801);
        cityCode.put("天水", 101160901);
        cityCode.put("武都", 101161001);
        cityCode.put("临夏", 101161101);
        cityCode.put("合作", 101161201);
        cityCode.put("白银", 101161301);
        cityCode.put("定西", 101160201);
        cityCode.put("张掖", 101160701);
        cityCode.put("广州", 101280101);
        cityCode.put("惠州", 101280301);
        cityCode.put("梅州", 101280401);
        cityCode.put("汕头", 101280501);
        cityCode.put("深圳", 101280601);
        cityCode.put("珠海", 101280701);
        cityCode.put("佛山", 101280800);
        cityCode.put("肇庆", 101280901);
        cityCode.put("湛江", 101281001);
        cityCode.put("江门", 101281101);
        cityCode.put("河源", 101281201);
        cityCode.put("清远", 101281301);
        cityCode.put("云浮", 101281401);
        cityCode.put("潮州", 101281501);
        cityCode.put("东莞", 101281601);
        cityCode.put("中山", 101281701);
        cityCode.put("阳江", 101281801);
        cityCode.put("揭阳", 101281901);
        cityCode.put("茂名", 101282001);
        cityCode.put("汕尾", 101282101);
        cityCode.put("韶关", 101280201);
        cityCode.put("南宁", 101300101);
        cityCode.put("柳州", 101300301);
        cityCode.put("来宾", 101300401);
        cityCode.put("桂林", 101300501);
        cityCode.put("梧州", 101300601);
        cityCode.put("防城港", 101301401);
        cityCode.put("贵港", 101300801);
        cityCode.put("玉林", 101300901);
        cityCode.put("百色", 101301001);
        cityCode.put("钦州", 101301101);
        cityCode.put("河池", 101301201);
        cityCode.put("北海", 101301301);
        cityCode.put("崇左", 101300201);
        cityCode.put("贺州", 101300701);
        cityCode.put("贵阳", 101260101);
        cityCode.put("安顺", 101260301);
        cityCode.put("都匀", 101260401);
        cityCode.put("兴义", 101260906);
        cityCode.put("铜仁", 101260601);
        cityCode.put("毕节", 101260701);
        cityCode.put("六盘水", 101260801);
        cityCode.put("遵义", 101260201);
        cityCode.put("凯里", 101260501);
        cityCode.put("昆明", 101290101);
        cityCode.put("红河", 101290301);
        cityCode.put("文山", 101290601);
        cityCode.put("玉溪", 101290701);
        cityCode.put("楚雄", 101290801);
        cityCode.put("普洱", 101290901);
        cityCode.put("昭通", 101291001);
        cityCode.put("临沧", 101291101);
        cityCode.put("怒江", 101291201);
        cityCode.put("香格里拉", 101291301);
        cityCode.put("丽江", 101291401);
        cityCode.put("德宏", 101291501);
        cityCode.put("景洪", 101291601);
        cityCode.put("大理", 101290201);
        cityCode.put("曲靖", 101290401);
        cityCode.put("保山", 101290501);
        cityCode.put("呼和浩特", 101080101);
        cityCode.put("乌海", 101080301);
        cityCode.put("集宁", 101080401);
        cityCode.put("通辽", 101080501);
        cityCode.put("阿拉善左旗", 101081201);
        cityCode.put("鄂尔多斯", 101080701);
        cityCode.put("临河", 101080801);
        cityCode.put("锡林浩特", 101080901);
        cityCode.put("呼伦贝尔", 101081000);
        cityCode.put("乌兰浩特", 101081101);
        cityCode.put("包头", 101080201);
        cityCode.put("赤峰", 101080601);
        cityCode.put("南昌", 101240101);
        cityCode.put("上饶", 101240301);
        cityCode.put("抚州", 101240401);
        cityCode.put("宜春", 101240501);
        cityCode.put("鹰潭", 101241101);
        cityCode.put("赣州", 101240701);
        cityCode.put("景德镇", 101240801);
        cityCode.put("萍乡", 101240901);
        cityCode.put("新余", 101241001);
        cityCode.put("九江", 101240201);
        cityCode.put("吉安", 101240601);
        cityCode.put("武汉", 101200101);
        cityCode.put("黄冈", 101200501);
        cityCode.put("荆州", 101200801);
        cityCode.put("宜昌", 101200901);
        cityCode.put("恩施", 101201001);
        cityCode.put("十堰", 101201101);
        cityCode.put("神农架", 101201201);
        cityCode.put("随州", 101201301);
        cityCode.put("荆门", 101201401);
        cityCode.put("天门", 101201501);
        cityCode.put("仙桃", 101201601);
        cityCode.put("潜江", 101201701);
        cityCode.put("襄樊", 101200201);
        cityCode.put("鄂州", 101200301);
        cityCode.put("孝感", 101200401);
        cityCode.put("黄石", 101200601);
        cityCode.put("咸宁", 101200701);
        cityCode.put("成都", 101270101);
        cityCode.put("自贡", 101270301);
        cityCode.put("绵阳", 101270401);
        cityCode.put("南充", 101270501);
        cityCode.put("达州", 101270601);
        cityCode.put("遂宁", 101270701);
        cityCode.put("广安", 101270801);
        cityCode.put("巴中", 101270901);
        cityCode.put("泸州", 101271001);
        cityCode.put("宜宾", 101271101);
        cityCode.put("内江", 101271201);
        cityCode.put("资阳", 101271301);
        cityCode.put("乐山", 101271401);
        cityCode.put("眉山", 101271501);
        cityCode.put("凉山", 101271601);
        cityCode.put("雅安", 101271701);
        cityCode.put("甘孜", 101271801);
        cityCode.put("阿坝", 101271901);
        cityCode.put("德阳", 101272001);
        cityCode.put("广元", 101272101);
        cityCode.put("攀枝花", 101270201);
        cityCode.put("银川", 101170101);
        cityCode.put("中卫", 101170501);
        cityCode.put("固原", 101170401);
        cityCode.put("石嘴山", 101170201);
        cityCode.put("吴忠", 101170301);
        cityCode.put("西宁", 101150101);
        cityCode.put("黄南", 101150301);
        cityCode.put("海北", 101150801);
        cityCode.put("果洛", 101150501);
        cityCode.put("玉树", 101150601);
        cityCode.put("海西", 101150701);
        cityCode.put("海东", 101150201);
        cityCode.put("海南", 101150401);
        cityCode.put("济南", 101120101);
        cityCode.put("潍坊", 101120601);
        cityCode.put("临沂", 101120901);
        cityCode.put("菏泽", 101121001);
        cityCode.put("滨州", 101121101);
        cityCode.put("东营", 101121201);
        cityCode.put("威海", 101121301);
        cityCode.put("枣庄", 101121401);
        cityCode.put("日照", 101121501);
        cityCode.put("莱芜", 101121601);
        cityCode.put("聊城", 101121701);
        cityCode.put("青岛", 101120201);
        cityCode.put("淄博", 101120301);
        cityCode.put("德州", 101120401);
        cityCode.put("烟台", 101120501);
        cityCode.put("济宁", 101120701);
        cityCode.put("泰安", 101120801);
        cityCode.put("西安", 101110101);
        cityCode.put("延安", 101110300);
        cityCode.put("榆林", 101110401);
        cityCode.put("铜川", 101111001);
        cityCode.put("商洛", 101110601);
        cityCode.put("安康", 101110701);
        cityCode.put("汉中", 101110801);
        cityCode.put("宝鸡", 101110901);
        cityCode.put("咸阳", 101110200);
        cityCode.put("渭南", 101110501);
        cityCode.put("太原", 101100101);
        cityCode.put("临汾", 101100701);
        cityCode.put("运城", 101100801);
        cityCode.put("朔州", 101100901);
        cityCode.put("忻州", 101101001);
        cityCode.put("长治", 101100501);
        cityCode.put("大同", 101100201);
        cityCode.put("阳泉", 101100301);
        cityCode.put("晋中", 101100401);
        cityCode.put("晋城", 101100601);
        cityCode.put("吕梁", 101101100);
        cityCode.put("乌鲁木齐", 101130101);
        cityCode.put("石河子", 101130301);
        cityCode.put("昌吉", 101130401);
        cityCode.put("吐鲁番", 101130501);
        cityCode.put("库尔勒", 101130601);
        cityCode.put("阿拉尔", 101130701);
        cityCode.put("阿克苏", 101130801);
        cityCode.put("喀什", 101130901);
        cityCode.put("伊宁", 101131001);
        cityCode.put("塔城", 101131101);
        cityCode.put("哈密", 101131201);
        cityCode.put("和田", 101131301);
        cityCode.put("阿勒泰", 101131401);
        cityCode.put("阿图什", 101131501);
        cityCode.put("博乐", 101131601);
        cityCode.put("克拉玛依", 101130201);
        cityCode.put("拉萨", 101140101);
        cityCode.put("山南", 101140301);
        cityCode.put("阿里", 101140701);
        cityCode.put("昌都", 101140501);
        cityCode.put("那曲", 101140601);
        cityCode.put("日喀则", 101140201);
        cityCode.put("林芝", 101140401);
        cityCode.put("台北县", 101340101);
        cityCode.put("高雄", 101340201);
        cityCode.put("台中", 101340401);
        cityCode.put("海口", 101310101);
        cityCode.put("三亚", 101310201);
        cityCode.put("东方", 101310202);
        cityCode.put("临高", 101310203);
        cityCode.put("澄迈", 101310204);
        cityCode.put("儋州", 101310205);
        cityCode.put("昌江", 101310206);
        cityCode.put("白沙", 101310207);
        cityCode.put("琼中", 101310208);
        cityCode.put("定安", 101310209);
        cityCode.put("屯昌", 101310210);
        cityCode.put("琼海", 101310211);
        cityCode.put("文昌", 101310212);
        cityCode.put("保亭", 101310214);
        cityCode.put("万宁", 101310215);
        cityCode.put("陵水", 101310216);
        cityCode.put("西沙", 101310217);
        cityCode.put("南沙岛", 101310220);
        cityCode.put("乐东", 101310221);
        cityCode.put("五指山", 101310222);
        cityCode.put("琼山", 101310102);
        cityCode.put("长沙", 101250101);
        cityCode.put("株洲", 101250301);
        cityCode.put("衡阳", 101250401);
        cityCode.put("郴州", 101250501);
        cityCode.put("常德", 101250601);
        cityCode.put("益阳", 101250700);
        cityCode.put("娄底", 101250801);
        cityCode.put("邵阳", 101250901);
        cityCode.put("岳阳", 101251001);
        cityCode.put("张家界", 101251101);
        cityCode.put("怀化", 101251201);
        cityCode.put("黔阳", 101251301);
        cityCode.put("永州", 101251401);
        cityCode.put("吉首", 101251501);
        cityCode.put("湘潭", 101250201);
        cityCode.put("南京", 101190101);
        cityCode.put("镇江", 101190301);
        cityCode.put("苏州", 101190401);
        cityCode.put("南通", 101190501);
        cityCode.put("扬州", 101190601);
        cityCode.put("宿迁", 101191301);
        cityCode.put("徐州", 101190801);
        cityCode.put("淮安", 101190901);
        cityCode.put("连云港", 101191001);
        cityCode.put("常州", 101191101);
        cityCode.put("泰州", 101191201);
        cityCode.put("无锡", 101190201);
        cityCode.put("盐城", 101190701);
        cityCode.put("哈尔滨", 101050101);
        cityCode.put("牡丹江", 101050301);
        cityCode.put("佳木斯", 101050401);
        cityCode.put("绥化", 101050501);
        cityCode.put("黑河", 101050601);
        cityCode.put("双鸭山", 101051301);
        cityCode.put("伊春", 101050801);
        cityCode.put("大庆", 101050901);
        cityCode.put("七台河", 101051002);
        cityCode.put("鸡西", 101051101);
        cityCode.put("鹤岗", 101051201);
        cityCode.put("齐齐哈尔", 101050201);
        cityCode.put("大兴安岭", 101050701);
        cityCode.put("长春", 101060101);
        cityCode.put("延吉", 101060301);
        cityCode.put("四平", 101060401);
        cityCode.put("白山", 101060901);
        cityCode.put("白城", 101060601);
        cityCode.put("辽源", 101060701);
        cityCode.put("松原", 101060801);
        cityCode.put("吉林", 101060201);
        cityCode.put("通化", 101060501);
        cityCode.put("沈阳", 101070101);
        cityCode.put("鞍山", 101070301);
        cityCode.put("抚顺", 101070401);
        cityCode.put("本溪", 101070501);
        cityCode.put("丹东", 101070601);
        cityCode.put("葫芦岛", 101071401);
        cityCode.put("营口", 101070801);
        cityCode.put("阜新", 101070901);
        cityCode.put("辽阳", 101071001);
        cityCode.put("铁岭", 101071101);
        cityCode.put("盘锦", 101071301);
        cityCode.put("大连", 101070201);
        cityCode.put("锦州", 101070701);
    }
}
